const goodsModel = require("../model/goods")
const historyModel = require("../model/history")
class Goods{
    static async getIndexJD(ctx){
        // 去redis价差下 看看 有没有这个列表存在
        let list = await ctx.redis.get("getIndexJD")
        if(list){
            list = JSON.parse(list)
        }else{
            list = await goodsModel.getIndexJD()
            await ctx.redis.set("getIndexJD",JSON.stringify(list),60)
        }
        ctx.body = {
            code:200,
            codeInfo:"成功",
            data:list
        }
    }

    static async getJDList(ctx){
        let {page} = ctx.query
        page=page?page:1
        // let list = await ctx.redis.get("getJDList"+page)
        // if(list){
            // list = JSON.parse(list)
        // }else{
           let list = await goodsModel.getJDList(page)
            // await ctx.redis.set("getJDList"+page,JSON.stringify(list),60)
        // }
        
        // let list = await goodsModel.getJDList(page)
        ctx.body = {
            code:200,
            codeInfo:"成功",
            data:list
        }
    }

    
    static async getIndexSouvenir(ctx){
        let list = await goodsModel.getIndexSouvenir()
        ctx.body = {
            code:200,
            codeInfo:"成功",
            data:list
        }
    }

    static async getSouvenirList(ctx){
        let {page} = ctx.query
        page=page?page:1
        let list = await goodsModel.getSouvenirList(page)
        ctx.body = {
            code:200,
            codeInfo:"成功",
            data:list
        }
    }

    static async goodsDetail(ctx){
        let {id}=ctx.query
        let res = await goodsModel.getGoodsDetail(id)
        ctx.body = {
            code:200,
            codeInfo:"成功",
            data:res
        }
    }
    /**
     * 获取我喜爱的列表
     * @param {*} ctx 
     */
    static async checkLike(ctx){
        //验证用户是否登录 如何验证就是看有没有token
        let users = ctx.jwt.check(ctx.header.authorization)
        if(users){
            let {goodsid} = ctx.request.body
            let res = await goodsModel.checkLike(users.id,goodsid)
            ctx.body={
                code:200,
                codeInfo:"成功",
                data:res
            }
        }else{
            ctx.body = {
                code:301,
                codeInfo:"用户尚未登录",
            }   
        }
        
    }

    static async likeFlag(ctx){
         let users = ctx.jwt.check(ctx.header.authorization)
        if(users){
            let flag = await goodsModel.likeFlag(users.id)
             ctx.body={
                code:200,
                codeInfo:"成功",
                data:flag
            }
        }else{
             ctx.body = {
                code:301,
                codeInfo:"用户尚未登录",
            }   
        }
    }

    static async getSql(ctx){
        let {page,limit}=ctx.query
        page=page?page:1
        limit=limit?limit:50
        let list = await ctx.redis.get("getSql"+page+limit)
        if(list){
            list = JSON.parse(list)
        }else{
            list = await goodsModel.createNewGoods(page,limit)
            await ctx.redis.set("getSql"+page+limit,JSON.stringify(list),60)
        }
        // let res = await goodsModel.createNewGoods(page,limit)
        // console.log(res)
        ctx.body=list
    
    }

    static async getPhotoSql(ctx){
        let {page,limit}=ctx.query
        page=page?page:1
        limit=limit?limit:50
        let res = await goodsModel.createNewGoodsPhoto(page,limit)
        console.log(res)
        ctx.body=res     
    }

    /**
     * 用户浏览商品详情满3秒后 方可记录为历史，历史显示的规则 是 访问次数越多 排位越靠前
     * LRU 淘汰机制 =》[1,2,3,4,5] =>5 =>[5,1,2,3,4] =>6 =>[6,5,1,2,3]
     */
    static async addHistory(ctx){
        let {id,uid} = ctx.query
        if(!id||!uid){
            ctx.body={
                code:201,
                codeInfo:"参数不足"
            }
        }else{
            // 调用添加的方法
            let res = await historyModel.add(id,uid)
            if(res){
                ctx.body={
                    code:200,
                    codeInfo:"成功"
                }
            }else{
                ctx.body={
                    code:202,
                    codeInfo:"参数错误"
                }
            }
        }
    }
    /**
     * 获取访问历史
     */
    static async getHistory(ctx){
        let {uid,page} = ctx.query
        if(!uid){
            ctx.body={
                code:201,
                codeInfo:"参数错误"
            }
        }else{
            page = page?page:1
            let list = ctx.redis.get(`getHistory_${uid}_${page}`)
            if(list){
                list = JSON.parse(list)
            }else{
                list = await historyModel.get(uid,page)
                ctx.redis.set(`getHistory_${uid}_${page}`,JSON.stringify(list),60)
            }
            ctx.body={
                code:200,
                codeInfo:"成功",
                data:list
            }

        }
    }
}
module.exports=Goods